app.controller('AccountCtrl',['$scope','$ionicActionSheet','$ionicHistory','$timeout','$state','appFactory',
    function($scope,$ionicActionSheet,$ionicHistory,$timeout,$state,appFactory) {
        var Courier = AV.Object.extend('Courier');
        /**
         * 用户信息初始化
         */
        $scope.accountInit=function(){
            $scope.user=appFactory.getUserInfo().user;
            $scope.$broadcast('scroll.refreshComplete');
        }
        $scope.accountInit();

        /**
         * 用户注销
         */
        $scope.loginOut=function(){
            appFactory.loginOut();
            $state.go('tab.take');
            window.location.reload();
            //$state.go('tab.take');
            //$ionicHistory.clearHistory();
        }
}]);
app.controller('AccountInfoCtrl',function($scope,$ionicActionSheet,$timeout,appFactory,$state){
    var storage = window.localStorage;
    var Courier = AV.Object.extend('Courier');
    /**
     * 基础信息初始化
     */
    $scope.useInfoInit=function(){
        $scope.user=appFactory.getUserInfo().user;
        $scope.$broadcast('scroll.refreshComplete');
    }
    $scope.useInfoInit();

    /**
     * 基础信息修改
     */
    //提示
    $scope.saveInfo = function() {
        var query=new AV.Query(Courier);
        query.get($scope.user.uid).then(function(res){
            res.set("phone",$scope.user.phone);
            res.set("email",$scope.user.email);
            res.save().then(function(res){
                $scope.user.phone=res.get('phone');
                $scope.user.email=res.get('email');
                //console.log($scope.user);
                var userJson=JSON.stringify($scope.user);
                storage.setItem("user",userJson);
                $state.go('tab.setting');
            }).catch(function(err){
                alert("保存失败！")
            });
        })
    }
    /**
     * 初始化当日派送清单
     */
    $scope.finishInit = function(){
        var uid = appFactory.getUserInfo().uid;
        var Order = AV.Object.extend("Order");
        var now = new Date();

        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日

        if(month<10){
            month = "0"+month;
        }
        if(day<10){
            day = "0"+day;
        }
        var rq = ""+year+"-"+month+"-"+day;

        var ORDER_STATUS = [2000,2500,3000,4000,7000,8000,9000,9999];
        var query = new AV.Query(Order);
        query.containedIn('status',ORDER_STATUS);
        query.equalTo("messionedUser",uid+"");
        //query.greaterThanOrEqualTo("endTime",new Date(rq+' 00:00:01'));
        query.greaterThanOrEqualTo("endTime",new Date(new Date(rq).toDateString() +' 00:00:00'));
        //query.lessThanOrEqualTo("endTime",new Date(rq+' 23:59:59'));
        query.lessThanOrEqualTo("endTime",new Date(new Date(rq).toDateString() +' 23:59:59'));
        query.addDescending("endTime");

        query.find().then(function(res){
            $scope.$apply(function(){
                $scope.ordersArray = res;
                $scope.ordersCount = res.length;
            });
        }).catch(function(err){
            $scope.ordersCount = 0;
        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    }
    $scope.finishInit();

    /**
     * 初始化未处理订单列表
     */
    $scope.neworderInit = function(){
        var uid = appFactory.getUserInfo().uid;
        var areaId = appFactory.getUserInfo().user.area;
        var Order = AV.Object.extend("Order");

        var ORDER_STATUS = [2500,3000,4000];
        var query = new AV.Query(Order);
        query.containedIn('status',ORDER_STATUS);
        query.containedIn("areaId",areaId);
        query.addDescending("updatedAt");

        query.find().then(function(res){
            $scope.$apply(function(){
                $scope.newordersArray = res;
                $scope.newordersCount = res.length;
            });
        }).catch(function(err){
            $scope.newordersCount = 0;
        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    }
    $scope.neworderInit();
})
app.controller('PasswordCtrl',function($scope,appFactory,$state,$timeout,$ionicModal){
    var Courier = AV.Object.extend('Courier');
    $scope.pwd=appFactory.getUserInfo().user;
    $scope.pwd.pwdfocus=true;
    $scope.pwd.repwdfocus=true;
    $scope.pwd.oldpwdfocus=true;

    $scope.lostFocus=function(arg){
        switch (arg){
            case 0:
                $scope.pwd.oldpwdfocus=false;
                break;
            case 1:
                $scope.pwd.pwdfocus=false;
                break;
            case 2:
                $scope.pwd.repwdfocus=false;
                break;
        }

        //$scope.pwd.pwdformate=false;
    }

    /**
     * 修改密码
     * @author Scor 2015-10-16
     */
    $scope.modifyPwd=function(){
        query=new AV.Query(Courier);
        query.equalTo("name",$scope.pwd.name);
        query.equalTo("pwd",md5($scope.pwd.oldpwd));
        query.first().then(function(res){
            if(res){
                res.set('pwd',md5($scope.pwd.newpwd));
                res.set('status',"2");
                res.save().then(function(){
                    alert("密码修改成功");
                    $scope.pwd.oldpwd="";
                    $scope.pwd.newpwd="";
                    $scope.pwd.renewpwd="";
                    $state.go('tab.setting');
                }).catch(function(){
                    alert("密码修改失败");
                });

            }else{
                alert("原密码不正确");
            }
        })

    }

    $scope.pwdSame=function(){
        if($scope.pwd.newpwd==$scope.pwd.renewpwd){
            $scope.pwd.pwdSame=true;
        }else{
            $scope.pwd.pwdSame=false;
        }
    }

})
